package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.model.param.AddMenuBarParam;
import com.tvunetworks.center.user.model.vo.MenuBarVo;
import com.tvunetworks.center.user.service.MenuBarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: MenuBarController
 * @author lebronchen
 * @create: 2019-06-11 10:57
 **/
@RestController
@RequestMapping("/menuBar")
@Slf4j
public class MenuBarController {

    @Autowired
    private MenuBarService menuBarService;

    /**
     *
     * @return Result
     */
    @GetMapping("/init")
    public Result<Object> initMenuBar(){
        menuBarService.initMenuBar();
        return ResultUtil.success();
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/add")
    public Result<Object> addMenuBar(@Valid AddMenuBarParam param){
        int result = menuBarService.addMenuBar(param);
        if (result == 1) {
            return ResultUtil.error("81000901", "Menu exist");
        } else if (result == 2) {
            return ResultUtil.error("81000902", "Feature not exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param id id
     * @param param param
     * @return Result
     */
    @PostMapping("/update")
    public Result<Object> updateMenuBar(@RequestParam String id, @Valid AddMenuBarParam param){
        int result = menuBarService.updateMenuBar(id, param);
        if (result == 1) {
            return ResultUtil.error("81001001", "Menu not exist");
        } else if (result == 2) {
            return ResultUtil.error("81001002", "Feature not exist");
        } else if (result == 3) {
            return ResultUtil.error("81001003", "Menu exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param id id
     * @return Result
     */
    @PostMapping("/delete")
    public Result<Object> deleteMenuBar(@RequestParam String id){
        menuBarService.deleteMenuBar(id);
        return ResultUtil.success();
    }

    /**
     *
     * @return Result
     */
    @GetMapping("/list")
    public Result<Object> listMenuBar(){
        List<MenuBarVo> list = menuBarService.listMenuBar();
        return ResultUtil.success(list);
    }

    /**
     *
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return Result
     */
    @GetMapping("/page")
    public Result<Object> pageMenuBar(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageInfo<MenuBarVo> pageInfo = menuBarService.pageMenuBar(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }
}
